<template>
  <el-dialog
    :title="!dataForm.id ? '新增模型' : '修改模型'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="应用标识" prop="clientId">
        <el-input v-model="dataForm.clientId" placeholder="应用标识"  v-bind:readonly="signreadonly"></el-input>
      </el-form-item>
      <el-form-item label="资源限定串" prop="resourceIds">
        <el-input v-model="dataForm.resourceIds" placeholder="资源限定串"></el-input>
      </el-form-item>
      <el-form-item label="应用密钥" prop="clientSecretStr" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.clientSecretStr" type="password" placeholder="应用密钥"></el-input>
      </el-form-item>
      <el-form-item label="确认应用密钥" prop="comfirmclientSecretStr" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmclientSecretStr" type="password" placeholder="确认应用密钥"></el-input>
      </el-form-item>
      <el-form-item label="范围" prop="scope">
        <el-input v-model="dataForm.scope" placeholder="范围"></el-input>
      </el-form-item>
      <el-form-item label="授权方式" prop="authorizedGrantTypes">
        <el-input v-model="dataForm.authorizedGrantTypes" placeholder="授权方式"></el-input>
      </el-form-item>
      <el-form-item label="回调地址 " prop="webServerRedirectUri">
        <el-input v-model="dataForm.webServerRedirectUri" placeholder="回调地址 "></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="authorities">
        <el-input v-model="dataForm.authorities" placeholder="权限"></el-input>
      </el-form-item>
      <el-form-item label="自动授权范围" prop="autoapprove">
        <el-input v-model="dataForm.autoapprove" placeholder="是否自动授权 是-true"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label=0>禁用</el-radio>
          <el-radio :label=1>正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('应用密钥不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认应用密钥不能为空'))
        } else if (this.dataForm.clientSecretStr !== value) {
          callback(new Error('确认应用密钥与应用密钥输入不一致'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        signreadonly: true,
        dataForm: {
          id: 0,
          clientId: '',
          resourceIds: '',
          clientSecret: '',
          clientSecretStr: '',
          comfirmclientSecretStr: '',
          scope: '',
          authorizedGrantTypes: '',
          webServerRedirectUri: '',
          authorities: '',
          accessTokenValidity: '',
          refreshTokenValidity: '',
          additionalInformation: '',
          autoapprove: '',
          status: 1,
          ifLimit: '',
          limitCount: ''
        },
        dataRule: {
          clientId: [
            { required: true, message: '应用标识不能为空', trigger: 'blur' }
          ],
          scope: [
            { required: true, message: '范围不能为空', trigger: 'blur' }
          ],
          authorizedGrantTypes: [
            { required: true, message: '授权方式不能为空', trigger: 'blur' }
          ],
          clientSecretStr: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmclientSecretStr: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.signreadonly = false
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/oauthclientdetails/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clientId = data.data.clientId
                this.dataForm.resourceIds = data.data.resourceIds
                this.dataForm.scope = data.data.scope
                this.dataForm.authorizedGrantTypes = data.data.authorizedGrantTypes
                this.dataForm.webServerRedirectUri = data.data.webServerRedirectUri
                this.dataForm.authorities = data.data.authorities
                this.dataForm.autoapprove = data.data.autoapprove
                this.dataForm.status = data.data.status
                this.signreadonly = true
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oauthclientdetails/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'clientId': this.dataForm.clientId,
                'resourceIds': this.dataForm.resourceIds,
                'clientSecretStr': this.dataForm.clientSecretStr,
                'scope': this.dataForm.scope,
                'authorizedGrantTypes': this.dataForm.authorizedGrantTypes,
                'webServerRedirectUri': this.dataForm.webServerRedirectUri,
                'authorities': this.dataForm.authorities,
                'autoapprove': this.dataForm.autoapprove,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
